﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Projecturo.ProjecturoClient.Command;
using System.Windows.Input;
using System.Windows;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Data;
using Projecturo.ProjecturoClient.ViewModel.Project;
using Projecturo.ProjecturoClient.ViewModel.Dashboard;

namespace Projecturo.ProjecturoClient.ViewModel
{
    public class MainWindowViewModel : WorkspaceViewModel, IWorkspace
    {
        private DelegateCommand showDashboardCommand;
        private DelegateCommand newProjectCommand;
        private ObservableCollection<WorkspaceViewModel> workspaces;

        public MainWindowViewModel()
        {
        }

        protected override void OnViewModelCreated()
        {
            base.OnViewModelCreated();

            CreateDashboard();
        }

        public override string DisplayName
        {
            get
            {
                return base.DisplayName;
            }
            protected set
            {
                base.DisplayName = value;
            }
        }

        public ICommand NewProjectCommand
        {
            get
            {
                if (this.newProjectCommand == null)
                    this.newProjectCommand = new DelegateCommand(NewProject);
                return this.newProjectCommand;
            }
        }

        private void NewProject()
        {
            ProjectViewModel projectViewModel = new ProjectViewModel();
            this.Workspaces.Add(projectViewModel);
            this.SetActiveWorkspace(projectViewModel);
        }

        public ICommand ShowDashboardCommand
        {
            get
            {
                if (this.showDashboardCommand == null)
                    this.showDashboardCommand = new DelegateCommand(ShowDashboard, CanShowDashboard);
                return this.showDashboardCommand;
            }
        }

        private void ShowDashboard()
        {
            this.CreateDashboard();
        }

        private bool CanShowDashboard()
        {
            foreach (WorkspaceViewModel workspace in this.Workspaces)
            {
                if (workspace is DashboardViewModel)
                    return false;
            }
            return true;
        }

        private void CreateDashboard()
        {
            DashboardViewModel dashboard = ViewModelManager.Instance.CreateViewModel<DashboardViewModel>(this);
            this.Workspaces.Add(dashboard);
            this.SetActiveWorkspace(dashboard);
        }

        void OnWorkspacesChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Count != 0)
                foreach (WorkspaceViewModel workspace in e.NewItems)
                    workspace.CloseRequested += this.OnWorkspaceCloseRequested;

            if (e.OldItems != null && e.OldItems.Count != 0)
                foreach (WorkspaceViewModel workspace in e.OldItems)
                    workspace.CloseRequested -= this.OnWorkspaceCloseRequested;
        }

        void OnWorkspaceCloseRequested(object sender, EventArgs e)
        {
            WorkspaceViewModel workspace = sender as WorkspaceViewModel;
            if (workspace != null
                && workspace.ViewModelReference.ParentViewModelId.Equals(this.ViewModelReference.CurrentViewModelId))
            {
                workspace.Dispose();
            }
            this.Workspaces.Remove(workspace);
        }

        public ObservableCollection<WorkspaceViewModel> Workspaces
        {
            get
            {
                if (this.workspaces == null)
                {
                    this.workspaces = new ObservableCollection<WorkspaceViewModel>();
                    this.workspaces.CollectionChanged += OnWorkspacesChanged;
                }
                return this.workspaces;
            }
        }

        public void AddWorkspace(WorkspaceViewModel workspaceViewModel, bool setActive)
        {
            this.Workspaces.Add(workspaceViewModel);
            this.SetActiveWorkspace(workspaceViewModel);
        }

        protected void SetActiveWorkspace(WorkspaceViewModel workspace)
        {
            ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.Workspaces);
            if (collectionView != null)
                collectionView.MoveCurrentTo(workspace);
        }

        protected WorkspaceViewModel GetActiveWorkspace()
        {
            ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.Workspaces);
            if (collectionView != null)
                return collectionView.CurrentItem as WorkspaceViewModel;
            return null;
        }
    }
}
